#include "../comm.h"
class Solution {
private:
    using PII = pair<int, int>;
public:
    int n, m;
    int maxIncreasingCells(vector<vector<int>>& mat) {
        n = mat.size(), m = mat[0].size();
        vector<int> row(n), col(m);
        map<int, vector<PII>> mp;
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < m; ++j)
                mp[mat[i][j]].push_back({i, j});

        int ans = 1;
        for(auto& [k, v]: mp)
        {
            // cout << k << endl;
            vector<int> res;
            for(auto& [x, y]: v)
                res.push_back(max(row[x], col[y]) + 1);
            for(int i = 0; i < v.size(); ++i)
            {
                auto& [x, y] = v[i];
                row[x] = max(row[x], res[i]);
                col[y] = max(col[y], res[i]);
                ans = max(ans, res[i]);
            }
        }
        // for(auto e: row) cout << e << ' '; cout << endl;
        // for(auto e: col) cout << e << ' '; cout << endl;
        return ans;
    }
};